﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Devices.Sensors;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;


namespace MoviTiendas.Sensores
{
    
    public class Acelerometro
    {

        private Accelerometer acel;
        private Page pagina;

        public Acelerometro(Page pag)
        {
            pagina = pag;
            acel = new Accelerometer();
            acel.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(acel_ReadingChanged);
            acel.Start();

        }

        void acel_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MiLecturaCambio(e));

        }

        void MiLecturaCambio(AccelerometerReadingEventArgs e)
        {
           // MessageBox.Show(pagina.Name);
            var currentPage = ((App)Application.Current).RootFrame.Content as PhoneApplicationPage;
            if (currentPage.Name.Equals("Principal"))
                return;

            if ( e.X < -0.6 && e.Z < -0.7)
            {
             //   MessageBox.Show("Todavia pasan cosas raras");
            //    pagina.NavigationService.GoBack();
            try{

                currentPage.NavigationService.GoBack();

            }
                catch{
                }
                
               // acel.Stop();
            }
        }

    }
}
